package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.OperUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.ExpPayReq;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.ReqList;
import com.softer.wxzj.mapper.ExpPayReqMapper;
import com.softer.wxzj.mapper.ExpProjectReqMapper;
import com.softer.wxzj.mapper.ExpProjectSearchMapper;
import com.softer.wxzj.service.ExpProjectSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/8/20
 */
@Service
public class ExpProjectSearchServiceImpl implements ExpProjectSearchService {
    @Autowired
    private ExpProjectSearchMapper expProjectSearchMapper;
    @Autowired
    private ExpProjectReqMapper expProjectReqMapper;
    @Autowired
    private ExpPayReqMapper expPayReqMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    private ExpPayReqServiceImpl expPayReqService;

    /**
     * 项目信息查询列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 项目列表
     */
    @Override
    public PageInfo<ExpProjectReq> getProjectList(Page page, ExpProjectReq expProjectReq) {
        //查询登陆账号的管理区域
        String zoneCode = zoneCodeUtil.getZoneCode();
        expProjectReq.setZoneCode(zoneCode);
        List<ExpProjectReq> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isNotEmpty(expProjectReq.getFuzzy())) {
            //模糊查詢
            list = expProjectSearchMapper.fuzzyProjectList(expProjectReq);
        } else {
            //精确查询
            list = expProjectSearchMapper.getProjectList(expProjectReq);
        }
        for (ExpProjectReq projectReq : list) {
            if (projectReq.getFinalSum().compareTo(BigDecimal.ZERO) == 0) {
                projectReq.setSysAmount(projectReq.getGovernable());
                projectReq.setFinishSelfAmount(projectReq.getSelfAmount());
            }
            //判断质保金是否到期，并且是否已经返回
            dealLastFlag(projectReq);
            //查询项目的最后一次审批的审批类型
            String flowType = expProjectSearchMapper.getFlowType(projectReq.getId());
            projectReq.setFlowType(flowType);
        }
        return new PageInfo<>(list);
    }

    /**
     * 判断质保金是否到期，并且是否已经返回
     *
     * @param projectReq 项目信息
     */
    private void dealLastFlag(ExpProjectReq projectReq) {
        //质保金到期日
        String warrantyExpirationDate = projectReq.getWarrantyExpirationDate();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (StringUtils.isEmpty(warrantyExpirationDate)) {
            projectReq.setLastFlag(NumberEnum.ZERO.getNum());
        } else {
            LocalDate date = LocalDate.parse(warrantyExpirationDate, fmt);
            if (date.isAfter(LocalDate.now())) {
                projectReq.setLastFlag(NumberEnum.ZERO.getNum());
            } else {
                BigDecimal lastSum = projectReq.getLastSum();
                if (BigDecimal.ZERO.compareTo(lastSum) == 0) {
                    //已支付质保金是0，则说明质保金到期后，未支付
                    projectReq.setLastFlag(NumberEnum.TWO.getNum());
                } else {
                    //质保金到期，并且已经支付
                    projectReq.setLastFlag(NumberEnum.ONE.getNum());
                }
            }
        }
    }

    /**
     * 到期维修项目查询列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 项目列表
     */
    @Override
    public PageInfo<ExpProjectReq> getExpirationDateList(Page page, ExpProjectReq expProjectReq) {
        //查询登陆账号的管理区域
        String zoneCode = zoneCodeUtil.getZoneCode();
        expProjectReq.setZoneCode(zoneCode);
        List<ExpProjectReq> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isNotEmpty(expProjectReq.getFuzzy())) {
            //模糊查詢
            list = expProjectSearchMapper.fuzzyExpirationDateList(expProjectReq);
        } else {
            //精确查询
            list = expProjectSearchMapper.getExpirationDateList(expProjectReq);
        }
        PageInfo<ExpProjectReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 拨款、付款信息查询
     * (付款信息时，只查询已经记账的信息)
     *
     * @param expPayReq 查询实体
     * @return 拨款、付款信息
     */
    @Override
    public ReqList expPayReqInfoList(ExpPayReq expPayReq, String arcType) {
        ReqList reqList = new ReqList();
        List<ExpPayReq> expPayReqInfoList;
        if (StringUtils.isNotEmpty(expPayReq.getFuzzy())) {
            expPayReqInfoList = expProjectSearchMapper.expPayReqInfoListFuzzy(expPayReq.getReqId(), expPayReq.getFuzzy());
        } else {
            expPayReqInfoList = expProjectSearchMapper.expPayReqInfoList(expPayReq);
        }
        //获取图档
        expPayReqService.getFileTypeList(expPayReqInfoList, arcType);
        if (expPayReqInfoList != null && !expPayReqInfoList.isEmpty()) {
            ExpPayReq total = new ExpPayReq();
            total.setReqName("合计");
            //申请金额
            total.setPayAmount(expPayReqInfoList.stream().map(ExpPayReq::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            //系统支付
            total.setSystemPayment(expPayReqInfoList.stream().map(ExpPayReq::getSystemPayment).reduce(BigDecimal.ZERO, BigDecimal::add));
            //现金支付
            total.setSelfAmount(expPayReqInfoList.stream().map(ExpPayReq::getSelfAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            //拨款比例
            total.setPayRate(expPayReqInfoList.stream().map(ExpPayReq::getPayRate).reduce(BigDecimal.ZERO, BigDecimal::add));
            expPayReqInfoList.add(total);
        }
        reqList.setExpPayReqList(expPayReqInfoList);
        reqList.setExpProjectReq(expProjectSearchMapper.expGrantInfoSum(expPayReq.getReqId()));
        return reqList;
    }

    @Override
    public ExpProjectReq expGrantInfoSum(String id) {
        ExpProjectReq expProjectReq = expProjectSearchMapper.expGrantInfoSum(id);
        if (NumStrEnum.SIX.getNum().equals(expProjectReq.getStep()) || NumStrEnum.FOUR.getNum().equals(expProjectReq.getStep())) {
            int num;
            if (expProjectReq.getIsLast() == 1) {
                //决算拨款
                num = expProjectReq.getPayCount() - 1;
            } else {
                //决算拨款
                num = expProjectReq.getPayCount();
            }
            ExpPayReq expPayReq = expPayReqMapper.getLastSumById(id, num);
            if (expPayReq != null) {
                if (NumStrEnum.ONE.getNum().equals(expPayReq.getFlowState()) || NumStrEnum.TWO.getNum().equals(expPayReq.getFlowState())) {
                    expProjectReq.setStep(NumStrEnum.SEVEN.getNum());
                }
            }
        }
        return expProjectReq;
    }

    /**
     * 使用查询流程图对应当前项目的状态
     *
     * @return
     */
    @Override
    public String step(String id) {
        ExpProjectReq expProjectReq = expProjectReqMapper.getInfoById(id);
        String step = expProjectReq.getStep();
        if (StringUtils.isNotEmpty(step)) {
            if (step.equals(String.valueOf(NumberEnum.FIVE.getNum()))) {
                //对于流程图来说 审核通过&不通过 均是审核完成
                step = String.valueOf(NumberEnum.FOUR.getNum());
            }
        }
        return step;
    }
}
